<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div>进度总览</div>
        <div class="flex">
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        v-loading="loading"
        :row-style="{
          height:
            height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
        @expand-change="expandChange"
        row-key="id"
        :expand-row-keys="expands"
        ref="tables"
      >
        <el-table-column type="expand">
          <template #default="props">
            <div m="4 " class="p_10" style="background: rgb(240, 242, 245)">
              <h4>进度详情：</h4>
              <el-table
                class="tc m_top10"
                :data="props.row.detailList"
                border
                max-height="450"
                :span-method="objectSpanMethod"
              >
                <el-table-column label="任务名称" prop="taskName">
                </el-table-column>
                <el-table-column
                  label="子任务名称"
                  prop="taskNodeName"
                ></el-table-column>
                <el-table-column label="分配人" prop="memberName">
                </el-table-column>
                <el-table-column
                  label="当前进度"
                  prop="completionRate"
                ></el-table-column>
                <el-table-column label="开始时间" prop="taskNodeName">
                  <template #default="scope">
                    <div v-if="scope.row.actualStartTime ?? '' !== ''">
                      {{
                        formatDate(
                          parseInt(scope.row.actualStartTime),
                          'YYYY-MM-DD'
                        )
                      }}
                    </div>
                    <div v-else>-- --</div>
                  </template></el-table-column
                >
                <el-table-column label="结束时间" prop="taskNodeName">
                  <template #default="scope">
                    <div v-if="scope.row.actualFinishTime ?? '' !== ''">
                      {{
                        formatDate(
                          parseInt(scope.row.actualFinishTime),
                          'YYYY-MM-DD'
                        )
                      }}
                    </div>
                    <div v-else>-- --</div>
                  </template></el-table-column
                >
                <el-table-column
                  label="节点工时"
                  prop="usedHours"
                ></el-table-column>
                <el-table-column label="状态">
                  <template #default="scope">
                    <div v-if="scope.row.status == 1">未开始</div>
                    <div v-if="scope.row.status == 2">
                      进行中<span v-if="scope.row.isCancel">(已取消)</span>
                    </div>
                    <div v-if="scope.row.status == 3">
                      延期<span v-if="scope.row.isCancel">(已取消)</span>
                    </div>
                    <div v-if="scope.row.status == 4">
                      已完成<span v-if="scope.row.isCancel">(已取消)</span>
                    </div>
                    <div v-if="scope.row.status == 5">超时完成</div>
                  </template>
                </el-table-column>
                <el-table-column
                  label="逾期天数"
                  prop="overDays"
                ></el-table-column>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="项目名称"
          prop="name"
          key="项目名称"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="设计经理"
          prop="managerName"
          key="设计经理"
          sortable
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="设计组长"
          prop="groupLeaderName"
          key="设计组长"
          sortable
          v-if="header_list[2].showTable"
        ></el-table-column>
        <el-table-column
          label="设计部门"
          prop="deptName"
          key="设计部门"
          sortable
          v-if="header_list[3].showTable"
        ></el-table-column>
        <el-table-column
          label="设计成员"
          prop="projectMember"
          key="设计成员"
          sortable
          v-if="header_list[4].showTable"
        ></el-table-column>

        <el-table-column
          label="计划开始日期"
          prop="planStartTime"
          key="计划开始日期"
          sortable
          v-if="header_list[5].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.planStartTime ?? '' !== ''">
              {{ formatDate(parseInt(scope.row.planStartTime), 'YYYY-MM-DD') }}
            </div>
            <div v-else>-- --</div>
          </template>
        </el-table-column>
        <el-table-column
          label="计划完成日期"
          prop="planEndTime"
          key="计划完成日期"
          sortable
          v-if="header_list[6].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.planEndTime ?? '' !== ''">
              {{ formatDate(parseInt(scope.row.planEndTime), 'YYYY-MM-DD') }}
            </div>
            <div v-else>-- --</div>
          </template>
        </el-table-column>
        <el-table-column
          label="总完成度"
          prop="completionRate"
          key="总完成度"
          sortable
          v-if="header_list[7].showTable"
        >
        </el-table-column>
        <el-table-column
          label="当前总工时"
          prop="usedHours"
          key="当前总工时"
          sortable
          v-if="header_list[8].showTable"
        >
          <template #default="scope">
            <div>
              {{ scope.row.usedHours }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="当前项目状态"
          key="当前项目状态"
          sortable
          prop="isDelete"
          v-if="header_list[9].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.status == 1">进行中</div>
            <div v-if="scope.row.status == 2">资料已提交</div>
            <div v-if="scope.row.status == 3">三层完成</div>
          </template></el-table-column
        >
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="tables.toggleRowExpansion(scope.row)"
              >进度详情</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  check_role,
  formatDate,
  debounce,
  handle_search_key,
} from '@/utils/check_role' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { ElMessage } from 'element-plus'
const list = ref()
const total = ref()
const max_height = ref()
const loading = ref(false)
const height_size = ref(1)
const tables = ref(null)
const expands = ref([])
const expandChange = (e, expandedRows) => {
  console.log(e)
  if (expandedRows.length) {
    expands.value = []
    if (e) {
      expands.value.push(e.id) // 每次push进去的是每行的ID
    }
  } else {
    expands.value = [] // 默认不展开
  }
  if (!expands.value.length) return
}
const header_list = ref<any>([
  {
    label: '项目名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '设计经理',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '设计组长',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '设计部门',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '设计成员',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '计划开始时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },
  {
    label: '计划结束时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: false,
    options: [],
  },

  {
    label: '总完成度',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '当前总工时',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '当前项目状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: '1',
        label: '进行中',
      },
      {
        value: '2',
        label: '资料已提交',
      },
      {
        value: '3',
        label: '三层完成',
      },
    ],
  },
])
const page_data = ref<any>({
  current: 1,
  pageSize: 10,
  name: '',
  managerName: '',
  groupLeaderName: '',
  deptName: '',
  projectMember: '',

  planStartTime: '',
  planEndTime: '',
  completionRate: '',
  usedHours: '',
  status: '',
})

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    if (row.all_nodes) {
      return {
        rowspan: row.all_nodes,
        colspan: 1,
      }
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      }
    }
  }
}
onMounted(() => {
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  get_list()
})
onUnmounted(() => {
  EventBus.off('refresh')
  EventBus.off('change_default_active')
})

const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.taskProcessPage,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      list.value = res.data.content
      res.data.content.forEach((i) => {
        if (i.detailList.length) {
          let task_num = 0
          i.detailList.forEach((ii, index2) => {
            if (index2 == 0) {
              i.detailList[task_num].all_nodes = 1
            } else {
              if (ii.taskName == i.detailList[index2 - 1].taskName) {
                i.detailList[task_num].all_nodes += 1
              } else if (ii.taskName != i.detailList[index2 - 1].taskName) {
                task_num = index2
                i.detailList[task_num].all_nodes = 1
              }
            }
          })
        }
      })

      console.log(res)
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
let list_debounce = debounce(200, get_list)
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    name: header_list.value[0].value,
    managerName: header_list.value[1].value,
    groupLeaderName: header_list.value[2].value,
    deptName: header_list.value[3].value,
    projectMember: header_list.value[4].value,
    planStartTime: header_list.value[5].value,
    planEndTime: header_list.value[6].value,
    completionRate: header_list.value[7].value,
    usedHours: header_list.value[8].value,
    status: header_list.value[9].value,
  }
  get_list()
}
</script>

<style lang="scss" scoped>
.top_item {
  border: 1px solid #ccc;
  cursor: pointer;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  &:hover {
    color: #3580eb;
    border-color: #3580eb;
  }
}
.line {
  background-color: #333;
  width: 1px;
  height: 16px;
  margin: 0 10px;
}
</style>
